package com.example.understand_compose1.testcase_activity

import android.content.DialogInterface
import android.os.Bundle
import android.view.View
import android.widget.Toast
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import com.example.understand_compose1.R
import com.example.understand_compose1.databinding.ActivityFetMultiChoiceDialogBinding

class FetMultiChoiceDialogActivity : AppCompatActivity() {
    lateinit var binding:ActivityFetMultiChoiceDialogBinding


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        binding = ActivityFetMultiChoiceDialogBinding.inflate(layoutInflater)

        setContentView(binding.root)

        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }
    }


    val hobbyarr = arrayOf("唱歌", "跳舞", "吃鸡")
    val isselectarr = booleanArrayOf(false, false, true)

    fun showMultiChoiceDialog(view: View) {
        val ad = AlertDialog.Builder(view.context) // 使用 view.context 替代 this，以避免上下文混淆
            .setTitle("请选择兴趣爱好")
//            TODO: 傻逼操作啊!!
            .setMultiChoiceItems(hobbyarr, isselectarr) { dialog, which, isChecked ->
                // 在这里处理每个项目的选中状态变化
                // which 是被点击项目的索引，isChecked 是新的选中状态
                // 例如，可以在这里更新 isselectarr 数组
                isselectarr[which] = isChecked

            }
            .setPositiveButton("确定") {
                dialog, which ->
                val sb = StringBuffer()
                for (j in 0..<isselectarr.size) {
                    if (isselectarr[j]) {
                        sb.append(hobbyarr[j]).append(" ")
                    }
                }

                Toast.makeText(this,sb.toString(),Toast.LENGTH_SHORT).show()
                sb.setLength(0)
            }
            .setNegativeButton("取消", null)
            // 创建 AlertDialog 实例
            .create()
            // 显示 AlertDialog
            .show()
    }
}